#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash lastz-1.04.03.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget


set -e

cd $buildpath
wget -t 10 -c https://github.com/lastz/lastz/archive/refs/tags/1.04.03.tar.gz -O lastz-1.04.03.tar.gz
tar -zxvf $buildpath/lastz-1.04.03.tar.gz -C $buildpath && rm -rf $buildpath/lastz-1.04.03.tar.gz

cd $buildpath/lastz-1.04.03
sed -i '4s:gcc:clang:g' src/Makefile
sed -i '57s: -Werror : :g' src/Makefile
mkdir -p $installpath/lastz-1.04.03
export LASTZ_INSTALL=$installpath/lastz-1.04.03/bin
make -j$(nproc)
make -j$(nproc) install

cat>"$installpath/lastz-1.04.03/lastz_modulefiles"<<EOF
#%Module1.0
conflict lastz
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set LASTZ \$pwd
setenv LASTZ \$LASTZ
prepend-path PATH \$LASTZ/bin
EOF

rm -rf $buildpath/lastz-1.04.03
